<?php 
namespace App\Http\Controllers\ds;
use App\Http\Controllers\Controller;
use DB;
class DsController extends Controller
{
	public function zc()
	{
		return view('ds.zhuce');
	}
	public function log()
	{
		$user_name=$_POST['user_name'];
		$user_pwd=$_POST['user_pwd'];
		if (empty($user_name)) {
			echo '<script>alert("数据不能为空")</script>';
			return view('ds.zhuce');
		}else if(empty($user_pwd)){
			echo '<script>alert("数据不能为空")</script>';
			return view('ds.zhuce');
		}else{
			$res=DB::table('shop_user')->insert(['user_name'=>$user_name,'user_pwd'=>$user_pwd]);
		if ($res) {
			echo '<script>alert("注册成功");location.href="login"</script>';
		}
		}
		// return view('day1.login');
	}
	public function login()
	{
		return view('ds.login');
	}
	public function log_do()
	{
		$user_name=$_POST['user_name'];
		$user_pwd=$_POST['user_pwd'];
		$res=DB::table('shop_user')->where(['user_name'=>$user_name,'user_pwd'=>$user_pwd])->get();
		// print_r($res);die;
		$a=json_decode($res);
		// print_r($res);die;
		if (!empty($a)) {
			echo '<script>alert("登录成功");location.href="show"</script>';
		}else{
			echo '<script>alert("登录失败");location.href="log"</script>';
		}
	}
	public function show()
	{
		// echo 1;die;
		header("content-type:text/html;charset=utf-8");
		$res=DB::table('role')->get();
		// print_r($data);die;
		return view('Day1.show',['data'=>$res]);
	}
	public function add()
	{
		return view('day1.add');
	}
	public function add_do()
	{
		$name=$_POST['name'];
		$age=$_POST['age'];
		$sex=$_POST['sex'];
		$res=DB::table('role')->insert(['name'=>$name,'age'=>$age,'sex'=>$sex]);
		// print_r($res);die;
		if ($res) {
			return redirect('Day1/show');
		}
	}
	public function delete()
	{
		$id=$_GET['id'];
		$res=DB::table('role')->where('id',$id)->delete();
		if ($res) {
			return redirect('Day1/show');
		}
	}
	public function upe()
	{
		$id=$_GET['id'];
		$res=DB::table('role')->where('id',$id)->first();
		return view('Day1.update',['res'=>$res]);
	}
	public function update()
	{
		$id=$_POST['id'];
		$name=$_POST['name'];
		$age=$_POST['age'];
		$sex=$_POST['sex'];
		$arr=array('id'=>$id,'name'=>$name,'age'=>$age,'sex'=>$sex);
		$res=DB::table('role')->where('id','=',$id)->update($arr);
		if ($res) {
			return redirect('Day1/show');
		}
	}
}